package com.ccs.blockchain.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.ccs.blockchain.eneity.ParamObj;
import com.ccs.blockchain.signer.Signer;
import com.ccs.blockchain.signer.SignerException;

public abstract class BaiduFeedController {
	
	private static final String DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
	
	protected abstract StringEntity genHttpEntity(ParamObj paramObj);

	protected String genUtc() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
	    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
	    String utcTime = sdf.format(new Date());
	    return utcTime;
	}
	
	protected String service(ParamObj paramObj, String uri) {
    	String utc = genUtc();
    	CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("http://sem.baidubce.com" + uri);
        httppost.addHeader("Host", "sem.baidubce.com");
        httppost.addHeader("Content-Type", "application/json");
        httppost.addHeader("x-bce-date", utc);
        httppost.addHeader("authorization", genSignature(paramObj.getAccessKey(), paramObj.getSecretKey(), utc, uri));
        httppost.setEntity(genHttpEntity(paramObj));

        String result = null;
        try {
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return result;
	}
	
	protected String genSignature(String ak, String sk, String utc, String uri) {
    	Signer signer = new Signer();
        signer.withAccessKey(ak)
                .withSecretKey(sk)
                .withTimestamp(utc)
                .withMethod("POST")
                .withVersion("1")
                .withExpire(1800)
                .withUri(uri)
                .withSignHeaders(createHeaders(utc));
        String authorization = "";
        try {
            authorization = signer.genAuthorization();
        } catch (SignerException e) {
            System.out.println(e.getMessage());
        }
    	return authorization;
    }
	
	protected Map<String, String> createHeaders(String utcTime) {
        Map<String, String> headers = new HashMap<String, String>();
//        headers.put("x-bce-date", utcTime);
        headers.put("host", "sem.baidubce.com");
        return headers;
    }
}
